IF EXISTS (SELECT * FROM sysobjects WHERE type = 'P' AND name = 'spTletters_RetrieveAll')
	BEGIN
		PRINT 'Dropping Procedure spTletters_RetrieveAll'
		DROP  Procedure  spTletters_RetrieveAll
	END

GO

PRINT 'Creating Procedure spTletters_RetrieveAll'
GO

CREATE Procedure spTletters_RetrieveAll
	(
		@letterid int = null, 
		@ilpnumber varchar(50) = null, 
		@letterreference varchar(50) = null, 
		@letternotes varchar(500) = null, 
		@letteruser varchar(50) = null, 
		@senttoprimaryemail varchar(50) = null, 
		@senttosecondaryemail varchar(50) = null
	)
AS
    /* 
    ------------------------------------------------------------------------------
    - Change History
    - --------------
    - Date          Description
    ------------------------------------------------------------------------------
    - 8/23/2007	Initial version created
    ------------------------------------------------------------------------------
    */
	
	SET NOCOUNT ON
	
	-- Retrieve the relevant records
	SELECT  *
	FROM	Tletters
	
	WHERE	(@letterid IS NULL OR Tletters.letterid = @letterid)
	  AND	(@ilpnumber IS NULL OR Tletters.ilpnumber LIKE @ilpnumber + '%')
	  AND	(@letterreference IS NULL OR Tletters.letterreference LIKE @letterreference + '%')
	  AND	(@letternotes IS NULL OR Tletters.letternotes LIKE @letternotes + '%')
	  AND	(@letteruser IS NULL OR Tletters.letteruser LIKE @letteruser + '%')
	  AND	(@senttoprimaryemail IS NULL OR Tletters.senttoprimaryemail LIKE @senttoprimaryemail + '%')
	  AND	(@senttosecondaryemail IS NULL OR Tletters.senttosecondaryemail LIKE @senttosecondaryemail + '%')

	-- Return to calling method
	RETURN
GO
